package com.zkingsoft.wechat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.web.BaseAction;
import com.zkingsoft.bean.BizStaff;
import com.zkingsoft.bean.BizUser;
import com.zkingsoft.redis.RedisUserLoginUtils;
import com.zkingsoft.service.BizStaffService;
import com.zkingsoft.service.BizUserService;

/**
 * @description 商铺相关接口
 * @author 姜友瑶
 * @date 2018-05-23 00:08
 */
@Controller
@RequestMapping(value = "/wxapi/staff")
public class WxStaffAction extends BaseAction {

	@Autowired
	private BizUserService userService;

	@Autowired
	private BizStaffService staffService;

	@Autowired
	RedisUserLoginUtils redisUserLoginUtils;

	/**
	 * 查询店铺员工不分页
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年7月16日
	 * @param bizStaff
	 * @return
	 */
	@RequestMapping(value = "/findStaff")
	public @ResponseBody AjaxResult findStaff(@RequestBody BizStaff bizStaff) {
		// 店铺id必须传
		return new AjaxResult(AjaxResult.STATUS_SUCCESS, staffService.findByModel(bizStaff));
	}

	/**
	 * 检查用户是否可以被创建为员工
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年9月4日
	 * @param openId
	 * @return
	 */
	@RequestMapping(value = "/checkStaff/{openId}")
	public @ResponseBody AjaxResult findStaff(@PathVariable String openId) {
		BizUser loginUser = redisUserLoginUtils.getLoginUser();

		if (openId.equals(loginUser.getOpenId())) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, "不能绑定自己");
		}
		BizStaff tempStaff = staffService.findByUserOpenId(openId);
		if (tempStaff != null) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, "该用户已经绑定门店");
		} else {
			AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, "");
			BizUser user = userService.findByOpenId(openId);
			result.putInMap("name", user.getNickName());
			result.putInMap("img", user.getAvatarUrl());
			return result;
		}

	}

	/**
	 * 删除店员
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年7月16日
	 * @param staffId
	 * @return
	 */
	@RequestMapping(value = "/delStaff/{staffId}")
	public @ResponseBody AjaxResult delStaff(@PathVariable String staffId) {
		int i = staffService.removeById(staffId);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, "删除成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL, "删除失败");
		}
	}

	/**
	 * 新增员工
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年7月16日
	 * @param bizStaff
	 * @return
	 */
	@RequestMapping(value = "/addStaff")
	public @ResponseBody AjaxResult addStaff(@RequestBody BizStaff bizStaff) {
		BizUser loginUser = redisUserLoginUtils.getLoginUser();
		
		// 检查用户是否已经存在
		BizStaff tempStaff = staffService.findByUserOpenId(bizStaff.getUserOpenId());
		
		if (tempStaff != null) {
			return new AjaxResult(AjaxResult.STATUS_FAIL, "该用户已经绑定门店");
		} else {

			bizStaff.setCreateBy(loginUser.getNickName());
			bizStaff.setUpdateBy(loginUser.getNickName());
			int i = staffService.add(bizStaff);
			if (i > 0) {
				return new AjaxResult(AjaxResult.STATUS_SUCCESS, "添加成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_FAIL, "添加失败");
			}
		}

	}

	/**
	 * 修改员工信息
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年7月16日
	 * @param bizStaff
	 * @return
	 */
	@RequestMapping(value = "/modifyStaff")
	public @ResponseBody AjaxResult modifyStaff(@RequestBody BizStaff bizStaff) {
		int i = staffService.modifyByModel(bizStaff);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, "修改成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL, "修改失败");
		}
	}

}